package com.dabay.web.job;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.dabay.web.module.Arear_lishouyd;
import com.dabay.web.module.Arear_renli;
import com.dabay.web.module.Arear_shanghaij;
import com.dabay.web.module.Arear_shebaok;
import com.dabay.web.module.Arear_yanglao;
import com.dabay.web.module.Arear_yiliao;
import com.dabay.web.module.Arear_yiliaojg;
import com.dabay.web.service.Area_service;
import com.dabay.web.util.JsonPluginsUtil;
import com.dabay.web.util.NbwxUtil;
import com.dabay.web.util.TimeUtil;

/**
 * 
 * @author chenghongjun
 *  
 *  
 * 同步内容：
 * 1 市区城镇医保定点医疗机构
 * 2 宁波市特病及转院资格定点医疗机构数据
 * 3 人力资源和社会保障局数据
 * 4 上海及杭州主城区的省、市医保定点三级医疗机构数据
 * 5 养老保险经办机构数据
 * 6 市区城镇医保定点医疗机构数据
 * 7 医疗保险经办机构数据
 * 8 社保卡服务机构数据
 *
 */
@Component
public class MQInitQuartz_Arear {
	
	private static Logger logger = LoggerFactory.getLogger(MQInitQuartz_Arear.class);
	
	@Autowired
	private Area_service area_service;


	@Scheduled(cron = "0 07 1 ? * *")
	public void runFunction() {
		sendMQsms_1();
		sendMQsms_2();
		sendMQsms_3();
		sendMQsms_4();
		sendMQsms_5();
		sendMQsms_6();
		sendMQsms_7();
		sendMQsms_8();
	}
	
	private void sendMQsms_1() {
		logger.info(new Date() + " 开始执行推送任务 --> 市区城镇医保定点医疗机构数据");
		String methods = "getSQCZYBDDLSYDList";
		String key = "";
		String result = "";
		try {
			result = NbwxUtil.getModelResult_listService(key, methods);
			if (StringUtils.isNotEmpty(result)) {
				try {
					List<com.dabay.web.module.Arear_lishouyd> arear_lishouyd_list = JsonPluginsUtil
							.jsonToBeanList(result,com.dabay.web.module.Arear_lishouyd.class);
					if(arear_lishouyd_list.size()>0){
						int thisNewReocrdCount = 0;
						for (Arear_lishouyd arear_lishouyd : arear_lishouyd_list) {
							try {
								String yljgmc = arear_lishouyd.getYljgmc();
								int count = area_service.selectArear_lishouyd(yljgmc);
								if (count == 0) {
									thisNewReocrdCount++;//记录数加1
									String create_time = TimeUtil.transTime();
									arear_lishouyd.setCreate_time(create_time);
									area_service.insertArear_lishouyd(arear_lishouyd);
								}								
							} catch (Exception e) {
								logger.info("error!! the data insert shibai == the Wcmmetatableddlsydid is"+arear_lishouyd.getWcmmetatableddlsydid());
								e.printStackTrace();
								logger.info(new Date() + " 插入数据库失败 --> 市区城镇医保定点医疗机构数据");
							}
						}
						logger.info(new Date() + " 数据库更新数 --> "+thisNewReocrdCount+"市区城镇医保定点医疗机构数据");
					}					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info(new Date() + " 结束执行推送任务 --> 市区城镇医保定点医疗机构数据");
	}
	
	private void sendMQsms_2() {
		// 宁波市特病及转院资格定点医疗机构
		logger.info(new Date() + " 开始执行推送任务 --> 宁波市特病及转院资格定点医疗机构数据");
		String methods = "getNBSTBJZYZGDDYLJGList";
		String key = "";
		String result = "";
		try {
			result = NbwxUtil.getModelResult_listService(key, methods);
			if (StringUtils.isNotEmpty(result)) {
				try {
					List<com.dabay.web.module.Arear_shanghaij> arear_shanghaij_list = JsonPluginsUtil
							.jsonToBeanList(result,com.dabay.web.module.Arear_shanghaij.class);
					if(arear_shanghaij_list.size()>0){
						int thisNewReocrdCount = 0;
						for (Arear_shanghaij arear_shanghaij : arear_shanghaij_list) {
							try {
								String doctitle = arear_shanghaij.getDoctitle();
								int count = area_service.selectArear_shanghaij_nb(doctitle);
								if (count == 0) {
									thisNewReocrdCount++;//记录数加1
									String create_time = TimeUtil.transTime();
									arear_shanghaij.setCreate_time(create_time);
									area_service.insertArear_shanghaij_nb(arear_shanghaij);
								}								
							} catch (Exception e) {
								logger.info("error!! the data insert shibai == the docid is"+arear_shanghaij.getDocid());
								e.printStackTrace();
								logger.info(new Date() + " 插入数据库失败 --> 宁波市特病及转院资格定点医疗机构数据");
							}
						}
						logger.info(new Date() + " 数据库更新数 --> "+thisNewReocrdCount+"宁波市特病及转院资格定点医疗机构数据");
					}					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	private void sendMQsms_3() {
		// 人力资源和社会保障局
		logger.info(new Date() + " 开始执行推送任务 --> 人力资源和社会保障局数据");
		String methods = "getRLZYSHBZJList";
		String key = "";
		String result = "";
		try {
			result = NbwxUtil.getModelResult_listService(key, methods);
			if (StringUtils.isNotEmpty(result)) {
				try {
					List<com.dabay.web.module.Arear_renli> arear_renli_list = JsonPluginsUtil
							.jsonToBeanList(result,com.dabay.web.module.Arear_renli.class);
					if(arear_renli_list.size()>0){
						int thisNewReocrdCount = 0;
						for (Arear_renli arear_renli : arear_renli_list) {
							try {
								String doctitle = arear_renli.getDoctitle();
								int count = area_service.selectArear_renli(doctitle);
								if (count == 0) {
									thisNewReocrdCount++;//记录数加1
									String create_time = TimeUtil.transTime();
									arear_renli.setCreate_time(create_time);
									area_service.insertArear_renli(arear_renli);
								}								
							} catch (Exception e) {
								e.printStackTrace();
								logger.info("error!! the data insert shibai == the docid is"+arear_renli.getDocid());
								logger.info(new Date() + " 插入数据库失败 --> 人力资源和社会保障局数据");
							}				
						}
						logger.info(new Date() + " 数据库更新数 --> "+thisNewReocrdCount+"人力资源和社会保障局数据");
					}					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info(new Date() + " 结束执行推送任务 --> 人力资源和社会保障局数据");
	}	
	private void sendMQsms_4() {
		// 上海及杭州主城区的省、市医保定点三级医疗机构
		logger.info(new Date() + " 开始执行推送任务 --> 上海及杭州主城区的省、市医保定点三级医疗机构数据");
		String methods = "getSHHZZCQYBSJYLJGList";
		String key = "";
		String result = "";
		try {
			result = NbwxUtil.getModelResult_listService(key, methods);
			if (StringUtils.isNotEmpty(result)) {
				try {
					List<com.dabay.web.module.Arear_shanghaij> arear_shanghaij_list = JsonPluginsUtil
							.jsonToBeanList(result,com.dabay.web.module.Arear_shanghaij.class);
					if(arear_shanghaij_list.size()>0){
						int thisNewReocrdCount = 0;
						for (Arear_shanghaij arear_shanghaij : arear_shanghaij_list) {
							try {
								String doctitle = arear_shanghaij.getDoctitle();
								int count = area_service.selectArear_shanghaij(doctitle);
								if (count == 0) {
									thisNewReocrdCount++;//记录数加1
									String create_time = TimeUtil.transTime();
									arear_shanghaij.setCreate_time(create_time);
									area_service.insertArear_shanghaij(arear_shanghaij);
								}								
							} catch (Exception e) {
								e.printStackTrace();
								logger.info("error!! the data insert shibai == the docid is"+arear_shanghaij.getDocid());
								logger.info(new Date() + " 插入数据库失败 --> 上海及杭州主城区的省、市医保定点三级医疗机构数据");
							}
						}
						logger.info(new Date() + " 数据库更新数 --> "+thisNewReocrdCount+"上海及杭州主城区的省、市医保定点三级医疗机构数据");
					}					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info(new Date() + " 结束执行推送任务 --> 上海及杭州主城区的省、市医保定点三级医疗机构数据");
	}
	private void sendMQsms_5() {
		//  养老保险经办机构
		logger.info(new Date() + " 开始执行推送任务 --> 养老保险经办机构数据");
		String methods = "getYLBXJBJGList";
		String key = "";
		String result = "";
		try {
			result = NbwxUtil.getModelResult_listService(key, methods);
			if (StringUtils.isNotEmpty(result)) {
				try {
					List<com.dabay.web.module.Arear_yanglao> arear_yanglao_list = JsonPluginsUtil
							.jsonToBeanList(result,com.dabay.web.module.Arear_yanglao.class);
					if(arear_yanglao_list.size()>0){
						int thisNewReocrdCount = 0;
						for (Arear_yanglao arear_yanglao : arear_yanglao_list) {
							try {
								String mc = arear_yanglao.getMc();
								int count = area_service.selectArear_yanglao(mc);
								if (count == 0) {
									thisNewReocrdCount++;//记录数加1
									String create_time = TimeUtil.transTime();
									arear_yanglao.setCreate_time(create_time);
									area_service.insertArear_yanglao(arear_yanglao);
								}								
							} catch (Exception e) {
								e.printStackTrace();
								logger.info("error!! the data insert shibai == the Metadataid is"+arear_yanglao.getMetadataid());
								logger.info(new Date() + " 插入数据库失败 --> 养老保险经办机构数据");
							}
						}
						logger.info(new Date() + " 数据库更新数 --> "+thisNewReocrdCount+"养老保险经办机构数据");
					}					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info(new Date() + " 结束执行推送任务 --> 养老保险经办机构数据");
	}
	private void sendMQsms_6() {
		//  市区城镇医保定点医疗机构
		logger.info(new Date() + " 开始执行推送任务 --> 市区城镇医保定点医疗机构数据");
		String methods = "getSQCZYBDDYLJGList";
		String key = "";
		String result = "";
		try {
			result = NbwxUtil.getModelResult_listService(key, methods);
			if (StringUtils.isNotEmpty(result)) {
				try {
					List<com.dabay.web.module.Arear_yiliaojg> arear_yiliaojg_list = JsonPluginsUtil
							.jsonToBeanList(result,com.dabay.web.module.Arear_yiliaojg.class);
					if(arear_yiliaojg_list.size()>0){
						int thisNewReocrdCount = 0;
						for (Arear_yiliaojg arear_yiliaojg : arear_yiliaojg_list) {
							try {
								String yljgmc = arear_yiliaojg.getYljgmc();
								int count = area_service.selectArear_yiliaojg(yljgmc);
								if (count == 0) {
									thisNewReocrdCount++;//记录数加1
									String create_time = TimeUtil.transTime();
									arear_yiliaojg.setCreate_time(create_time);
									area_service.insertArear_yiliaojg(arear_yiliaojg);
								}								
							} catch (Exception e) {
								e.printStackTrace();
								logger.info("error!! the data insert shibai == the Metadataid is"+arear_yiliaojg.getMetadataid());
								logger.info(new Date() + " 插入数据库失败 --> 市区城镇医保定点医疗机构数据");
							}
						}
						logger.info(new Date() + " 数据库更新数 --> "+thisNewReocrdCount+"市区城镇医保定点医疗机构数据");
					}					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info(new Date() + " 结束执行推送任务 --> 市区城镇医保定点医疗机构数据");
	}
	
	private void sendMQsms_7() {
		logger.info(new Date() + " 开始执行推送任务 --> 医疗保险经办机构数据");
		//  医疗保险经办机构
		String methods = "getYILBXJBJGList";
		String key = "";
		String result = "";
		try {
			result = NbwxUtil.getModelResult_listService(key, methods);
			if (StringUtils.isNotEmpty(result)) {
				try {
					List<com.dabay.web.module.Arear_yiliao> arear_yiliao_list = JsonPluginsUtil
							.jsonToBeanList(result,com.dabay.web.module.Arear_yiliao.class);
					if(arear_yiliao_list.size()>0){
						int thisNewReocrdCount = 0;
						for (Arear_yiliao arear_yiliao : arear_yiliao_list) {
							try {
								String dw = arear_yiliao.getDw();
								int count = area_service.selectArear_yiliao(dw);
								if (count == 0) {
									thisNewReocrdCount++;//记录数加1
									String create_time = TimeUtil.transTime();
									arear_yiliao.setCreate_time(create_time);
									area_service.insertArear_yiliao(arear_yiliao);
								}								
							} catch (Exception e) {
								e.printStackTrace();
								logger.info("error!! the data insert shibai == the Metadataid is"+arear_yiliao.getMetadataid());
								logger.info(new Date() + " 插入数据库失败 --> 医疗保险经办机构数据");
							}
						}
						logger.info(new Date() + " 数据库更新数 --> "+thisNewReocrdCount+"医疗保险经办机构数据");
					}					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info(new Date() + " 结束执行推送任务 --> 医疗保险经办机构数据");	
	}
	private void sendMQsms_8() {
		logger.info(new Date() + " 开始执行推送任务 --> 社保卡服务机构数据");
		//  社保卡服务机构
		String methods = "getSBKFWJGList";
		String key = "";
		String result = "";
		try {
			result = NbwxUtil.getModelResult_listService(key, methods);
			if (StringUtils.isNotEmpty(result)) {
				try {
					List<com.dabay.web.module.Arear_shebaok> arear_shebaok_list = JsonPluginsUtil
							.jsonToBeanList(result,com.dabay.web.module.Arear_shebaok.class);
					if(arear_shebaok_list.size()>0){
						int thisNewReocrdCount = 0;
						for (Arear_shebaok arear_shebaok : arear_shebaok_list) {
							try {
								String qx = arear_shebaok.getQx();
								int count = area_service.selectArear_shebaok(qx);
								if (count == 0) {
									thisNewReocrdCount++;//记录数加1
									String create_time = TimeUtil.transTime();
									arear_shebaok.setCreate_time(create_time);
									area_service.insertArear_shebaok(arear_shebaok);
								}								
							} catch (Exception e) {
								e.printStackTrace();
								logger.info("error!! the data insert shibai == the Metadataid is"+arear_shebaok.getMetadataid());
								logger.info(new Date() + " 插入数据库失败 --> 社保卡服务机构数据");
							}
						}
						logger.info(new Date() + " 数据库更新数 --> "+thisNewReocrdCount+"社保卡服务机构数据");
					}					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info(new Date() + " 结束执行推送任务 --> 社保卡服务机构数据");
	}

}
